package com.cf.api.db;

import com.md.common.utils.dao.mybatis.CfMybatisProperties;
import com.md.common.utils.dao.mybatis.MybatisProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 功能描述：Mybatis session配置
 *
 * @author lujianping@kuparts.com
 * @version 1.0
 * @date 2017/10/19 17:31
 * @since JDK 1.8
 */
@Configuration
public class MyBatisConfig {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Autowired
    @Qualifier("otherDataSource")
    private DataSource otherDataSource;

    @Autowired
    private MybatisProperties mybatisProperties;
    @Autowired
    private CfMybatisProperties cfMybatisProperties;

    @Primary
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        sessionFactory.setConfigLocation(applicationContext.getResource(mybatisProperties.getConfigLocation()));
        return sessionFactory;
    }

    @Primary
    @Bean("sqlSessionFactoryTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    // 配置事物管理器
    @Bean
    @Primary
    @Qualifier("transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactoryBean otherSqlSessionFactory(ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(otherDataSource);
        sessionFactory.setTypeAliasesPackage(cfMybatisProperties.getTypeAliasesPackage());
        sessionFactory.setConfigLocation(applicationContext.getResource(cfMybatisProperties.getConfigLocation()));
        return sessionFactory;
    }

    @Primary
    @Bean("otherSqlSessionTemplate")
    public SqlSessionTemplate otherSqlSessionTemplate(@Qualifier("otherSqlSessionFactory") SqlSessionFactory otherSqlSessionFactory) {
        return new SqlSessionTemplate(otherSqlSessionFactory);
    }

    // 配置事物管理器
    @Bean
    @Qualifier("otherTransactionManager")
    public DataSourceTransactionManager otherTransactionManager(@Qualifier("otherDataSource") DataSource otherDataSource){
        return new DataSourceTransactionManager(otherDataSource);
    }

    @Bean(name = "otherJdbcTemplate")
    public JdbcTemplate otherJdbcTemplate(@Qualifier("otherDataSource") DataSource otherDataSource) {
        return new JdbcTemplate(otherDataSource);
    }

}